 local HorseRankLogic = require("rank.HorseRankLogic")
local LvRankLogic = require("rank.LvRankLogic")
local PowerRankLogic = require("rank.PowerRankLogic")
local SbRankLogic = require("rank.SbRankLogic")
local PetRankLogic = require("rank.PetRankLogic")
local RankDefine = require("rank.RankDefine")
local Msg = require("core.Msg")
local LuaMongo = _G.lua_mongo
local DB = require("common.DB")
local Human = require("core.Human")
local RoleDBLogic = require("role.RoleDBLogic")
local RoleFFI = require("role.RoleFFI")
local StageConfig = require("excel.Horse").stage
local InnerDataManagerFW = require("innerdata.InnerDataManagerFW")
local InnerDataDefine = require("innerdata.Define")
local PataRankLogic = require("rank.PataRankLogic")
local PetExcel = require("excel.Pet").pet
local BaowuRankLogic = require("rank.BaowuRankLogic")
local BaowuConfig = require("excel.Baowu").baowu

function changeBlue(human, rank)
  if human then
    for i = 1, #rank do
      if human._id == rank[i].uuid then
        rank[i].blue = string.format("%d%d%d%d",human.db.blueVipLv, human.db.blueYearVip, human.db.blueSuperVip, human.db.level3366)
        break
      end
    end
  else
    for i=1, #rank do
      if string.len(rank[i].blue) == 3 then
        rank[i].blue = rank[i].blue .. "0"
      end      
    end    
  end  
end

function allChange(human)
  LvRankLogic.lvChange(human)
  PataRankLogic.PataChange(human)
  changeBlue(human, HorseRankLogic.HORSE_RANK)
  changeBlue(human, LvRankLogic.LEVEL_RANK)
  changeBlue(human, PowerRankLogic.POWER_RANK)
  changeBlue(human, SbRankLogic.SHENBING_RANK)
  changeBlue(human, PetRankLogic.PET_RANK)
  changeBlue(human, PataRankLogic.PATA_RANK)
  changeBlue(human, BaowuRankLogic.BAOWU_RANK)
end

function hotRank()
  changeBlue(nil, HorseRankLogic.HORSE_RANK)
  changeBlue(nil, LvRankLogic.LEVEL_RANK)
  changeBlue(nil, PowerRankLogic.POWER_RANK)
  changeBlue(nil, SbRankLogic.SHENBING_RANK)
  changeBlue(nil, PetRankLogic.PET_RANK)
  changeBlue(nil, PataRankLogic.PATA_RANK)
  changeBlue(nil, BaowuRankLogic.BAOWU_RANK)
end

function writeLog()
  PowerRankLogic.writeLog()
  SbRankLogic.writeLog()
  HorseRankLogic.writeLog()
  PetRankLogic.writeLog()
end

function CG_GET_COMMON_RANK_INFO(human, msg)
  local sendMsg = Msg.gc.GC_COMMON_RANK_LIST
  sendMsg.myRank = 0
  sendMsg.sblist[0] = 0
  sendMsg.powerlist[0] = 0
  sendMsg.horselist[0] = 0
  sendMsg.lvlist[0] = 0
  sendMsg.petlist[0] = 0
  sendMsg.baowulist[0] = 0
  if msg.type == RankDefine.LV_RANK_TYPE then
    LvRankLogic.GetRankList(human, sendMsg)
  elseif msg.type == RankDefine.POWER_RANK_TYPE then
    PowerRankLogic.GetRankList(human, sendMsg)
  elseif msg.type == RankDefine.SB_RANK_TYPE then
    SbRankLogic.GetRankList(human, sendMsg)
  elseif msg.type == RankDefine.HORSE_RANK_TYPE then
    HorseRankLogic.GetRankList(human, sendMsg)
  elseif msg.type == RankDefine.PET_RANK_TYPE then
    PetRankLogic.GetRankList(human, sendMsg)
  elseif msg.type == RankDefine.BAOWU_RANK_TYPE then
    BaowuRankLogic.GetRankList(human, sendMsg)
  end
end

function CG_GET_FIRST_RANK(human)
  local sendMsg = Msg.gc.GC_FIRST_RANK_LIST
  LvRankLogic.getFirst(human, sendMsg.lvlist)
  PowerRankLogic.getFirst(human, sendMsg.powerlist)
  SbRankLogic.getFirst(human, sendMsg.sblist)
  HorseRankLogic.getFirst(human, sendMsg.horselist)
  PetRankLogic.getFirst(human, sendMsg.petlist)
  BaowuRankLogic.getFirst(human, sendMsg.baowulist)
  Msg.send(sendMsg, human.fd)
end


function saveRank()
  LuaMongo.remove(DB.db_rank)
  LvRankLogic.saveRank()
  PowerRankLogic.saveRank()
  SbRankLogic.saveRank()
  HorseRankLogic.saveRank()
  PetRankLogic.saveRank()
  PataRankLogic.saveRank()
  BaowuRankLogic.saveRank()

  InnerDataManagerFW.Send2LInnerData(nil, InnerDataDefine.INNERDATA_TYPE_RANK_INFO, LvRankLogic.LEVEL_RANK)
  InnerDataManagerFW.Send2LInnerData(nil, InnerDataDefine.INNERDATA_TYPE_RANK_INFO, PowerRankLogic.POWER_RANK)
  InnerDataManagerFW.Send2LInnerData(nil, InnerDataDefine.INNERDATA_TYPE_RANK_INFO, SbRankLogic.SHENBING_RANK)
  InnerDataManagerFW.Send2LInnerData(nil, InnerDataDefine.INNERDATA_TYPE_RANK_INFO, HorseRankLogic.HORSE_RANK)
  InnerDataManagerFW.Send2LInnerData(nil, InnerDataDefine.INNERDATA_TYPE_RANK_INFO, PetRankLogic.PET_RANK)
end

function getRank()
  LvRankLogic.getRank()
  PowerRankLogic.getRank()
  SbRankLogic.getRank()
  HorseRankLogic.getRank()
  PetRankLogic.getRank()
  PataRankLogic.getRank()
  BaowuRankLogic.getRank()
  InnerDataManagerFW.Send2LInnerData(nil, InnerDataDefine.INNERDATA_TYPE_RANK_INFO, LvRankLogic.LEVEL_RANK)
  InnerDataManagerFW.Send2LInnerData(nil, InnerDataDefine.INNERDATA_TYPE_RANK_INFO, PowerRankLogic.POWER_RANK)
  InnerDataManagerFW.Send2LInnerData(nil, InnerDataDefine.INNERDATA_TYPE_RANK_INFO, SbRankLogic.SHENBING_RANK)
  InnerDataManagerFW.Send2LInnerData(nil, InnerDataDefine.INNERDATA_TYPE_RANK_INFO, HorseRankLogic.HORSE_RANK)
  InnerDataManagerFW.Send2LInnerData(nil, InnerDataDefine.INNERDATA_TYPE_RANK_INFO, PetRankLogic.PET_RANK)
end

function LW_CHECK_RANK_CHANGE(human, msg)
  if msg.type == RankDefine.LV_RANK_TYPE then
    return
  elseif msg.type == RankDefine.POWER_RANK_TYPE then
    PowerRankLogic.powerChange(nil,human.db)
  elseif msg.type == RankDefine.SB_RANK_TYPE then
    SbRankLogic.sbPowerChange(human)
  elseif msg.type == RankDefine.HORSE_RANK_TYPE then
    HorseRankLogic.horsePowerChange(human, msg.otherInfo)
  elseif msg.type == RankDefine.PET_RANK_TYPE then
    PetRankLogic.petPowerChange(human, msg.otherInfo)
  elseif msg.type == RankDefine.PATA_RANK_TYPE then
    PataRankLogic.PataChange(human)
  elseif msg.type == RankDefine.BAOWU_RANK_TYPE then
    BaowuRankLogic.baowuPowerChange(human, msg.otherInfo)
  end
end

function worship(human, oType, info)
  local obj = Human.onlineUuid[info.uuid]  
  if obj then
    obj.db.worshipedTimes = obj.db.worshipedTimes + 1
  else
    local db = RoleDBLogic.getDbByUuid(info.uuid)
    if not db then
      return
    end
    RoleDBLogic.saveRoleSset({_id = info.uuid, worshipedTimes = db.worshipedTimes + 1})
  end
  if RankDefine.PET_RANK_TYPE == info.type then
    checkInfo(human, info.uuid, RankDefine.PET_RANK_TYPE)
  else
    checkInfo(human, info.uuid)
  end
end
--得到神将id
function getPetId(petName)
  for i=1, #PetExcel do
    if PetExcel[i].petMingzi == petName then
      return i
    end
  end
end
--得到宝物id
function getBaowuId(baowuName)  
  for i=1, #BaowuConfig do
    if BaowuConfig[i].mingzi == baowuName then
      return i
    end
  end
end

function getBodyInfo(pet, id)
  local cf = PetExcel[id]
  return  cf.source,cf.weapon[1],0,cf.pifengId,cf.horse,0,cf.weapon[2],0
end

function checkInfo(human, uuid, rankDefine)
  local sendMsg = Msg.gc.GC_RANK_CHECK_OTHER
  local db = RoleDBLogic.getDbByUuid(uuid)
  sendMsg.worshipTimes = db.worshipedTimes
  sendMsg.familyName = RoleFFI.ffi_string(db.familyName)
  local baowuSource = 0
  if rankDefine and rankDefine == RankDefine.BAOWU_RANK_TYPE then
    for i=1, #BaowuRankLogic.BAOWU_RANK do
      if BaowuRankLogic.BAOWU_RANK[i].uuid == uuid then        
        baowuSource = BaowuConfig[getBaowuId(BaowuRankLogic.BAOWU_RANK[i].baowuName)].image
        break
      end
    end
  end
  local change = false
  if rankDefine and rankDefine == RankDefine.PET_RANK_TYPE and #PetRankLogic.PET_RANK >= 1 then
    for i=1, #PetRankLogic.PET_RANK do
      if PetRankLogic.PET_RANK[i].uuid == uuid then
        local petId = getPetId(PetRankLogic.PET_RANK[i].petName)
        if not petId or not PetExcel[petId] then
          return
        end
        Human.makeViewInfo(sendMsg.viewInfo, 0, getBodyInfo(nil, petId))
        change = true
        break
      end
    end    
  end
  if not change then
    Human.makeViewInfo(sendMsg.viewInfo, db.sex, Human.getRealBodyInfo(db, baowuSource))
  end
  Msg.send(sendMsg, human.fd)
end

function CG_RANK_CHECK_OTHER(human, msg)
  checkInfo(human, msg.uuid, msg.rankDefine)
end

function familyChange(uuid)
  local db = RoleDBLogic.getDbByUuid(uuid)
  LvRankLogic.familyChange(db)
  PowerRankLogic.familyChange(db)
end

function CG_GET_PATA_RANK(human)
  PataRankLogic.GetRankList(human)
end

function CG_PATA_FIRST_RANK(human)
  PataRankLogic.GetFirstRank(human)
end

InnerDataManagerFW.Register(InnerDataDefine.INNERDATA_TYPE_WOESHIP, worship)